package com.simple.portal.bo.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.simple.portal.bo.IMenuBO;
import com.simple.portal.dao.IMenuDAO;
import com.simple.portal.vo.MenuVO;
import com.simple.portal.vo.RightInfoVO;
import com.simple.util.MenuLoad;

@Service("menuBO")
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public class MenuBOImpl implements IMenuBO{


	private Map<String,MenuVO> menuMap;
	
	private List<MenuVO> allMenuVOList;
	
	@Autowired
	private IMenuDAO menuDAO;
	
	@Override
	public MenuVO findById(String id) {
		return menuDAO.findById(id);
	}

	@Override
	public List<MenuVO> findAll() {
		return menuDAO.findAll();
	}

	@Override
	public List<MenuVO> findAllMenuVO(){
		if(null == allMenuVOList){
			allMenuVOList=findAll();
		}
		return allMenuVOList;
	}
	
	
	@Override
	public Map<String,MenuVO> getAllMenuMap(){
		if(menuMap==null){
			buildMap();
		}
		return menuMap;
	}
	
	private void buildMap(){
		menuMap = new HashMap<String,MenuVO>();
		final List<MenuVO> menuVOList = findAll();
        final Iterator<MenuVO> iter =  menuVOList.iterator();
        while(iter.hasNext()){
            final MenuVO menuVO = (MenuVO)iter.next();
            menuMap.put(menuVO.getMenuId(), menuVO);
        }
	}

	@Override
	public Map<String,List<MenuVO>> getMenuByRight(final List<RightInfoVO> rightInfoVOList){
		Map<String,List<MenuVO>> menuMap =new LinkedHashMap<String,List<MenuVO>>();
		//get all the Menu information
		List<MenuVO> menuVOList=MenuLoad.getInstance().findAllMenuVO();

		List<MenuVO> parentMenuVOList=new ArrayList<MenuVO>();
		List<MenuVO> childMenuVOList=new ArrayList<MenuVO>();
		
		Map<String,MenuVO> tempMenuMap=MenuLoad.getInstance().getMenuMap();

		for(MenuVO menuVO : menuVOList ){
			 for(RightInfoVO rightInfoVO :rightInfoVOList ){
				 if(menuVO.getMenuId().equals(rightInfoVO.getMenuId())){
					 if(null == menuVO.getParentId()){
						 if (!parentMenuVOList.contains(menuVO)) {
								parentMenuVOList.add(menuVO);
							}
					 }else{
						 if(!childMenuVOList.contains(menuVO)){
							 childMenuVOList.add(menuVO);
						 }
						 MenuVO parmenuVO=tempMenuMap.get(menuVO.getParentId());
						 if(!parentMenuVOList.contains(parmenuVO)){
							 parentMenuVOList.add(parmenuVO);
						 }
					 }
					 
				 }
				 
			 }
		}
		
		Collections.sort(parentMenuVOList);
		Collections.sort(childMenuVOList);
		menuMap.put("parentMenuVOList", parentMenuVOList);
		menuMap.put("childMenuVOList", childMenuVOList);
		return menuMap;
		
	}
	
	
	@Override
	public Map<String,List<MenuVO>> getAllMenu(){
		final Map<String,List<MenuVO>> menuMap = new LinkedHashMap<String,List<MenuVO>>();
		final List<MenuVO> menuVOList = MenuLoad.getInstance().findAllMenuVO();

		final List<MenuVO> parentMenuVOList = new ArrayList<MenuVO>();
		final List<MenuVO> childMenuVOList = new ArrayList<MenuVO>();
		
		final Map<String,MenuVO> tempMenuMap = MenuLoad.getInstance().getMenuMap();

		for (MenuVO menuVO : menuVOList) {
			if (null == menuVO.getParentId()) {
				if (!parentMenuVOList.contains(menuVO)) {
					parentMenuVOList.add(menuVO);
				}
			} else {
				if (!childMenuVOList.contains(menuVO)) {
					childMenuVOList.add(menuVO);
				}
				final MenuVO parmenuVO = tempMenuMap.get(menuVO.getParentId());
				if (!parentMenuVOList.contains(parmenuVO)) {
					parentMenuVOList.add(parmenuVO);
				}

			}
		}
		
		Collections.sort(parentMenuVOList);
		Collections.sort(childMenuVOList);
		menuMap.put("parentMenuVOList", parentMenuVOList);
		menuMap.put("childMenuVOList", childMenuVOList);
		return menuMap;
		
	}
}
